import 'package:flutter/material.dart';
import 'package:flutter_massage/common_lib/extension/ex_widget.dart';
import 'package:flutter_massage/common_lib/index.dart';
import 'package:flutter_massage/common_lib/routers/route.dart';
import 'package:flutter_massage/generated/assets.dart';
import 'package:flutter_massage/pages/my/address/address_bean.dart';
import 'package:flutter_massage/pages/my/address/my_address_logic.dart';
import 'package:flutter_massage/pages/technician/choose_address/edit_address_page.dart';
import 'package:flutter_massage/values/index.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class MyAddressPage extends GetView<MyAddressLogic> {
  const MyAddressPage({super.key});

  @override
  Widget build(BuildContext context) {
    return BaseScaffold(
      titleStr: '我的地址',
      child: GetBuilder<MyAddressLogic>(
        init: MyAddressLogic(),
        assignId: true,
        builder: (logic) {
          return Stack(
            alignment: Alignment.bottomCenter,
            children: [
              ListView.separated(
                padding: EdgeInsets.only(bottom: ScreenUtil().bottomBarHeight + 60.w),
                itemCount: logic.list.length,
                separatorBuilder: (BuildContext context, int index) => CommonDivider(
                  marginLeft: 12.w,
                  marginRight: 12.w,
                ),
                itemBuilder: (BuildContext context, int index) {
                  return AddressItem(logic.list[index], onDelete: () => logic.deleteAddress(logic.list[index]));
                },
              ),
              ButtonWidget.text(
                '添加地址',
                onTap: () => CommonRoute.open(const EditAddressPage()),
                hasInkWell: false,
                alignment: Alignment.center,
                height: 50.w,
                gradient: CommonDecoration.xydGradient,
                borderRadius: 50.w,
                fontSize: 17,
                fontWeight: FontWeight.bold,
                textColor: AppColors.white,
              ).positioned(left: 12.w, right: 12.w, bottom: ScreenUtil().bottomBarHeight),
            ],
          );
        },
      ),
    );
  }
}

class AddressItem extends StatelessWidget {
  const AddressItem(this.bean, {super.key, this.onDelete, this.onTap});

  final AddressBean bean;

  final VoidCallback? onDelete;
  final VoidCallback? onTap;

  @override
  Widget build(BuildContext context) {
    return ButtonWidget(
      onTap: onTap,
      hasInkWell: false,
      child: Row(
        children: [
          Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              _buildAddress(),
              Row(
                children: [
                  _buildName().flexible(),
                  _buildPhone().paddingLeft(32.w),
                ],
              ).paddingTop(8.w),
            ],
          ).expanded(),
          _buildEdit().paddingLeft(5.w),
          if (onDelete != null) _buildDelete().paddingLeft(20.w),
        ],
      ).paddingAll(12.w),
    );
  }

  Widget _buildAddress() {
    return TextWidget.moreLine(
      text: bean.address ?? '',
      fontSize: 15,
      color: AppColors.textLevelOne,
      fontWeight: FontWeight.bold,
    );
  }

  Widget _buildName() {
    return TextWidget.oneLine(
      text: bean.name ?? '',
      fontSize: 12,
      color: AppColors.textLevelThree,
    );
  }

  Widget _buildPhone() {
    return TextWidget.oneLine(
      text: bean.phone.encryptPhone,
      fontSize: 12,
      color: AppColors.textLevelThree,
    );
  }

  Widget _buildEdit() {
    return ButtonWidget.icon(
      Assets.svgAddressEdit.toSvg(width: 15.w),
      onTap: () => CommonRoute.open(EditAddressPage(addressBean: bean)),
      hasInkWell: false,
    );
  }

  Widget _buildDelete() {
    return ButtonWidget.icon(
      Assets.svgAddressDelete.toSvg(width: 15.w),
      onTap: onDelete,
      hasInkWell: false,
    );
  }
}
